import VueResource from 'vue-resource';


export default {
    install(Vue) {

        /**
         * 展开对象
         * @param {*} target 目标
         * @param {*} parent 父级
         * @return {*} 返回展开的对象
         */
        Vue.unfold = function (target, parent) {
            if (target instanceof Object) {
                return Object.entries(target).reduce((p, u) => {
                    let key = parent ? `${parent}[${u[0]}]` : u[0],
                        result = this.unfold(u[1], key);
                    if (result instanceof Object) {
                        Object.assign(p, result);
                    } else {
                        p[key] = result;
                    }
                    return p;
                }, {});
            } else {
                return target;
            }
        }

        /*----------------------------------------------------------------------------------------------------*/

        /**
         * 延时器
         */
        Vue.timeout = Object.assign(Vue.prototype.$timeout = function (callback, duration = 0, id = 'default') {
            if (typeof duration == 'string') id = duration, duration = 0;
            id == 'default' || clearTimeout(this.$timeout.map[id]);
            this.$timeout.map[id] = setTimeout(() => {
                callback instanceof Function && callback();
                delete this.$timeout.map[id];
            }, duration);
        }, { map: {} });



        /**
         * 判断 是否 空数组，空对象，空字符串
        */
        Vue.prototype.$isempty = function(e)
        {
            return !e || e instanceof Array && !e.length;
        }
    }
}